package net.qhzw.geologr5.gis.service.system;

import lombok.extern.slf4j.Slf4j;
import net.qhzw.geologr5.gis.common.redis.utils.RedisUtils;
import net.qhzw.geologr5.gis.service.system.component.FileTemplate;
import net.qhzw.geologr5.gis.service.system.file2linux.ExtractUtils;
import net.qhzw.geologr5.gis.service.system.file2linux.SFTPUtil;
import net.qhzw.geologr5.gis.service.system.utils.TimeSimpleUtil;
import net.qhzw.geologr5.gis.service.system.utils.UUIDUtil;
import org.apache.http.entity.ContentType;
import org.csource.common.MyException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class UploadTest2 {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private FileTemplate fileTemplate;

    /***
     * 解压文件
     */
    @Test
    public void unZip() {
        long start = System.currentTimeMillis();
        String randomString = UUIDUtil.getRandomString(8); //随机文件夹
        String unZipDir = "/opt/upload/unZipDir/" + TimeSimpleUtil.getTimeStr() + "/" + randomString;
        //fileUrl: /opt/fastdfs/fdfs_storage/data/00/03/
        //fileName: enIOKmEQzoyAaCBsAADV_0lejhM275.zip
        String fileUrl = "/opt/aa/";
        String fileName = "金属矿预查报告.zip";

//        String fileUrl = "/opt/aa/";
//        String fileName = "金属矿预查报告.zip";


        // /opt/upload/unZipDir/20210809/8fapF68h/
        System.out.println("unZipDir: " + unZipDir);
        if (!SFTPUtil.isExistDir(unZipDir)) {
            Boolean mkdir1 = SFTPUtil.mkdir(unZipDir);
            if (!mkdir1) {
//                return JsonResult.error("服务器创建解压文件夹错误");
                System.out.println("服务器创建解压文件夹错误");
            }
        }
        //解压文件
        Map<String, Object> resultMap = ExtractUtils.remoteZipToFile(fileUrl, fileName, unZipDir);
        Boolean status = (Boolean) resultMap.get("status");
        List<String> dirList = (List<String>) resultMap.get("dirList");
        if (!status) {
            //return JsonResult.error("服务器解压文件出错");
            System.out.println("服务器解压文件出错");
        }
//        //解压后的路径
//        List<String> filePathList = conversion(dirList, unZipDir);
//        System.out.println("filePathList: " + filePathList);


        for (String s : dirList) {
            System.out.println("s:" + s);
        }

        long end = System.currentTimeMillis();

        System.out.println("解压耗时: " + (end - start) + "ms");
    }

    /***
     * 上传文件
     * @throws IOException
     * @throws MyException
     */
    @Test
    public void testUploadFile() throws IOException, MyException {
        long start = System.currentTimeMillis();
        String path = "D://tem//金属矿预查报告.zip";
        //file转 multipartFile
        File file = new File(path);
        FileInputStream inputStream = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);



        String filePath = fileTemplate.uploadFile(multipartFile);
        String fastDFSPath = "/opt/fastdfs/fdfs_storage/data/";
        System.out.println("filePath: " + filePath);
        Map<String, String> paramMap = subFilePath(filePath);
        String fileName = paramMap.get("fileName");
        String relativePath = paramMap.get("path");
        String fileUrl = fastDFSPath + relativePath;

        System.out.println("fileUrl: " + fileUrl);
        System.out.println("fileName: " + fileName);

        long end = System.currentTimeMillis();
        System.out.println("上传文件耗时：" + (end - start) + "ms");
    }


    public Map<String, String> subFilePath(String source) {
        Map<String, String> result = new HashMap<>();
        String[] split = source.split("/");
        StringBuffer sb = new StringBuffer();
        for (int i = 3; i < split.length; i++) {
            String s = split[i];
            if (i <= split.length - 2) {
                sb.append(s).append("/");
            }
            if (i == split.length - 1) {
                System.out.println("fileName" + split[i]);
                log.info("fileName: {}", split[i]);
                result.put("fileName", split[i]);
            }
        }
        result.put("path", sb.toString());
        return result;
    }

    private List<String> conversion(List<String> dirList, String sign) {
        System.out.println("dirList: " + dirList);
        System.out.println("sign: " + sign);
        List<String> result = new ArrayList<>();
        List<String> temList = new ArrayList<>();
        List<String> temList2 = new ArrayList<>();
        for (String path : dirList) {
            if (path.contains(sign) && !path.endsWith("/")) {
                temList.add(path);
            }
        }
        if (temList.isEmpty()) {
            return null;
        }
        for (String s : temList) {
            String tem = s.substring(0, s.indexOf(":"));
            String substring = s.substring(tem.length() + 1, s.length());
            temList2.add(substring);
        }

        for (String s : temList2) {
            String trim = s.trim();
            if (trim.startsWith("//")) {
                trim = trim.substring(1, trim.length());
            }
            if (!trim.endsWith("/")) {
                result.add(trim);
            }
        }
        return result;
    }

}
